package com.xkdx.guangguang.fragment.my.pay;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.xkdx.caipiao.R;
import com.xkdx.guangguang.fragment.BaseFragment;
import com.xkdx.guangguang.fragment.web.WebFragment;
import com.xkdx.guangguang.module.network.AbsModule;
import com.xkdx.guangguang.module.statics.IConstants;
import com.xkdx.guangguang.util.SharePrefenceUtil;

public class PayFragment extends BaseFragment implements OnClickListener {
	private View view;
	private ImageView pay_next;
	private Button back;
	private String CCB_URL = "";
	public static boolean isBack = false;
	private ImageView im_selected_pos, im_selected_ccb, im_selected_bank_card,
			im_selected_alipay_web, im_selected_pass_wealth,
			im_selected_wechat, im_selected_alipay;

	private List<ImageView> imList = new ArrayList<ImageView>();

	private RelativeLayout rl_pos, rl_ccb, rl_bank_card, rl_alipay_web,
			rl_pass_wealth, rl_wechat, rl_alipay;
	private int selectPos = 0;
	private SharePrefenceUtil sp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (getArguments() != null) {
			CCB_URL = getArguments().getString("paycode");
		}
		sp = new SharePrefenceUtil(getActivity(), IConstants.SP_USER);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (isBack) {
			getFragmentManager().popBackStack();
		}
		view = inflater.inflate(R.layout.pay_bank_choice, container, false);
		findView();
		setOnClick();
		return view;
	}

	@Override
	public void findView() {
		pay_next = (ImageView) view.findViewById(R.id.pay);
		back = (Button) view.findViewById(R.id.pay_bank_choice_back);
		rl_ccb = (RelativeLayout) view.findViewById(R.id.rl_ccb);
		rl_bank_card = (RelativeLayout) view.findViewById(R.id.rl_bank_card);
		rl_alipay = (RelativeLayout) view.findViewById(R.id.rl_alipay);
		rl_alipay_web = (RelativeLayout) view.findViewById(R.id.rl_alipay_web);
		rl_pass_wealth = (RelativeLayout) view
				.findViewById(R.id.rl_pass_wealth);
		rl_wechat = (RelativeLayout) view.findViewById(R.id.rl_wechat);
		rl_pos = (RelativeLayout) view.findViewById(R.id.rl_pos);

		im_selected_pos = (ImageView) view.findViewById(R.id.iv_pos);
		im_selected_ccb = (ImageView) view.findViewById(R.id.iv_ccb);
		im_selected_bank_card = (ImageView) view
				.findViewById(R.id.iv_bank_card);
		im_selected_alipay = (ImageView) view.findViewById(R.id.iv_alipay);
		im_selected_alipay_web = (ImageView) view
				.findViewById(R.id.iv_alipay_web);
		im_selected_pass_wealth = (ImageView) view
				.findViewById(R.id.iv_pass_wealth);
		im_selected_wechat = (ImageView) view.findViewById(R.id.iv_wechat);

		imList.add(im_selected_pos);
		imList.add(im_selected_ccb);
		imList.add(im_selected_bank_card);
		imList.add(im_selected_alipay);
		imList.add(im_selected_alipay_web);
		imList.add(im_selected_pass_wealth);
		imList.add(im_selected_wechat);
		String type = sp.getVerifyType();

		if (IConstants.POS_FROMWHERE.equals("danpin")) {

			if (type.equals("2") || type.equals("3")){
//					&& sp.getShopIds().contains(IConstants.POS_SHOPID)) {
				rl_pos.setVisibility(View.VISIBLE);
				rl_pos.setClickable(true);
			} else {

				rl_pos.setVisibility(View.GONE);
			}
		} else {

			rl_pos.setVisibility(View.GONE);

		}

	}

	@Override
	public void setOnClick() {

		pay_next.setOnClickListener(this);
		back.setOnClickListener(this);

		rl_ccb.setOnClickListener(this);
		rl_alipay.setOnClickListener(this);
		rl_alipay_web.setOnClickListener(this);
		rl_pass_wealth.setOnClickListener(this);
		rl_bank_card.setOnClickListener(this);
		rl_wechat.setOnClickListener(this);
		rl_pos.setOnClickListener(this);

	}

	@Override
	public void showLoading() {

	}

	@Override
	public void showLoaded(HashMap<String, AbsModule> map) {

	}

	@Override
	public void showClick(HashMap<String, AbsModule> map) {

	}

	@Override
	public void showLoadMore(HashMap<String, AbsModule> map) {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.rl_pos:
			imShow(im_selected_pos);
			selectPos = 1;
			// PayByPosFragment payByPosFragment = new PayByPosFragment();
			// getFragmentManager().beginTransaction()
			// .replace(R.id.fragment, payByPosFragment)
			// .addToBackStack(null).commit();
			imShow(im_selected_pos);
			break;

		case R.id.rl_ccb:
			selectPos = 2;
			imShow(im_selected_ccb);

			break;
		case R.id.rl_bank_card:
			selectPos = 3;
			imShow(im_selected_bank_card);
			break;
		case R.id.rl_alipay:
			selectPos = 4;
			imShow(im_selected_alipay);
			break;
		case R.id.rl_alipay_web:
			selectPos = 5;
			imShow(im_selected_alipay_web);
			break;
		case R.id.rl_pass_wealth:
			selectPos = 6;
			imShow(im_selected_pass_wealth);
			break;

		case R.id.rl_wechat:
			selectPos = 7;
			imShow(im_selected_wechat);
			break;

		case R.id.pay:
			if (selectPos == 1) {
				PayByPosFragment payByPosFragment = new PayByPosFragment();
				getFragmentManager().beginTransaction()
						.replace(R.id.fragment, payByPosFragment)
						.addToBackStack(null).commit();
			} else if (selectPos == 2) { // 建行支付
				if (TextUtils.isEmpty(CCB_URL)) {
					Toast.makeText(getActivity(), "此商品已售罄!", 0).show();
				} else {
					WebFragment webFragment = new WebFragment();
					Bundle b = new Bundle();
					b.putString("url", CCB_URL);
					webFragment.setArguments(b);
					getFragmentManager().beginTransaction()
							.replace(R.id.fragment, webFragment)
							.addToBackStack(null).commit();
				}

			}
			break;
		case R.id.pay_bank_choice_back:
			getFragmentManager().popBackStack();
			break;

		default:
			break;
		}

	}

	/**
	 * 判断选中状态
	 */
	private void imShow(ImageView view) {

		for (int i = 0; i < imList.size(); i++) {
			if (imList.get(i) == view) {
				imList.get(i).setImageResource(
						R.drawable.pay_choice_select_true);
			} else {
				imList.get(i).setImageDrawable(null);
			}
		}

	}

}
